//
//  AIRightCell.m
//  aixianfeng
//
//  Created by betty on 16/12/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIRightCell.h"
#import "AIProductsRightModel.h"
#import "AIMarketClickView.h"

@interface AIRightCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@property (weak, nonatomic) IBOutlet UILabel *nameLbl;
@property (weak, nonatomic) IBOutlet UILabel *specificsLbl;
@property (weak, nonatomic) IBOutlet UILabel *partnerLbl;
@property (weak, nonatomic) IBOutlet UILabel *marketLbl;
@property (weak, nonatomic) IBOutlet UIImageView *pmImg;


@property (nonatomic,strong) AIMarketClickView *clickView;
@end

@implementation AIRightCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    AIMarketClickView *clickView = [AIMarketClickView markerClickView];
    [self.contentView addSubview:clickView];
    _clickView = clickView;
    [clickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_marketLbl);
        make.right.equalTo(self.contentView).mas_offset(-8);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(23);
    }];
    
    //监听
    [clickView addTarget:self action:@selector(didClickView:) forControlEvents:UIControlEventValueChanged];
    
}


#pragma mark - 执行监听方法 -
- (void)didClickView: (AIMarketClickView *)clickView {
    // 为count赋值
    _rigthMdel.count = clickView.count;
    
    if (clickView.isIncrementClicked) {
//        NSLog(@"哎呀被点击 ++");
        
        CGPoint imgCenter = _iconImg.center;
        
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

        CGPoint convertPoint = [self.contentView convertPoint:imgCenter toView:keyWindow];
        
//        NSLog(@"%@",NSStringFromCGPoint(convertPoint));
        // 发出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kIncrementNotification object:_rigthMdel userInfo:@{@"point":[NSValue valueWithCGPoint:convertPoint],@"iconImage":_iconImg.image,@"iconBounds":[NSValue valueWithCGRect:_iconImg.bounds]}];
        
    }else {
        //NSLog(@"哎呀被点击 --");
    }
}


- (void)setRigthMdel:(AIProductsRightModel *)rigthMdel{
    _rigthMdel = rigthMdel;
    
    _nameLbl.text = rigthMdel.name;
    _specificsLbl.text = rigthMdel.specifics;
    _partnerLbl.text = rigthMdel.partner_price;
    _marketLbl.text = rigthMdel.market_price;
    
    //判断买一赠一
    if([rigthMdel.pm_desc isEqual:@""]) {
        _pmImg.hidden = YES;
    }else{
        _pmImg.hidden = NO;
    }
    
    NSString *urlString = [rigthMdel.img stringByDeletingPathExtension];
    NSURL *url = [NSURL URLWithString:urlString];
    [_iconImg sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"hehe"]];
 
   //解决重用问题
    
    _clickView.count = rigthMdel.count;
   
    
}

@end
